package com.example.kev;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class HelloTimePickerActivity extends Activity {
    
	private TextView mTimeDisplay;
	private Button mTimeChange;
	
	private int mHour;
	private int mMinute;
	private boolean twelveHourTime = true;
	
	private final int timeDialogId = 0;
	
	//create the listener for the TimePickerDialog
	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;		
			updateTimeDisplay();
		}
	};
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //Set main content view
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Capture textview and button
        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mTimeChange = (Button) findViewById(R.id.timeChange);
       
        //Set the on Click Listener for the button to show the display
        mTimeChange.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(0);
			}
		});
        
        //set time to current time
        final Calendar c = Calendar.getInstance(); 
        mMinute = c.get(Calendar.MINUTE);
        mHour = c.get(Calendar.HOUR);
        
        //update display to show default time
        updateTimeDisplay();
    }
    
	@Override
	protected Dialog onCreateDialog(int id) {
	    return new TimePickerDialog(this, timePickerListener, mHour, mMinute, twelveHourTime);
	}

  	public void updateTimeDisplay(){
  		mTimeDisplay.setText(new StringBuilder()
  			.append(pad(mHour)).append(":")
  			.append(pad(mMinute))
  		);
  	}
  	
  	private static String pad(int n){
  		if(n >= 10){
  			return String.valueOf(n);
  		}
  		return "0" + String.valueOf(n);
  	}

}